<?php
/**
 *	This file is part of rtfdoc.
 *
 *	rtfdoc is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation, either version 3 of the License, or
 *	(at your option) any later version.
 *
 *	rtfdoc is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License
 *	along with rtfdoc.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
class RTFBodyView
{
    /**
     * model to use
     *
     * @var RTFBody
     */
    public $model;
    /**
     * holds the RTF string
     *
     * @var string
     */
    private $rtf_text;
    /**
     * parses the input and returns the RTF string
     *
     * @param RTFBody $model
     * @return string
     */
    public function parse(RTFBody $model)
    {
        $this->model = $model;
        $this->rtf_text = $this->buildBody();
        return $this->rtf_text;
    }
    /**
     * builds the body output
     *
     * @return string
     */
    private function buildBody()
    {
    	$body_string = '\sectd\pard\plain %s \par';
    	$return = '';
        foreach ($this->model->getTexts() as $text)
        {
        	if($text instanceof RTFBody)
        	{
        		$return .= $text->getRTF();
        	}
        	else 
        	{
        		$return .= sprintf($body_string, $this->format($text));
        	}
        }
        return $return;
    }
    
    private function format($text)
    {
    	// FIXME: fix this right
    	$find = array('[b]','[/b]','[i]','[/i]');
    	$replace = array(' \b ',' \b0 ',' \i ',' \i0 ');
    	
    	return str_replace($find, $replace, $text);
    }
}
?>